/**
* ...
* @author Default
* @version 0.1
*/

package bdz.projects.slab.ui {
	import as3.common.ui.menu.core.MenuComponent;
	import as3.projects.slfi.data.ExhibitVO;
	import bdz.dms.data.DMSCollection;
	import bdz.dms.vo.SectionVO;
	import bdz.projects.slab.events.SlabSiteEvent;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import bdz.projects.slab.renderers.GalleryByMonth;
	import as3.common.ui.menu.TweenedGridList;
	import flash.events.Event;
	import as3.common.utils.BitmapUtils;
	import flash.text.TextFormat;
	import flash.events.MouseEvent;
	import as3.common.utils.TweenLite;
	import as3.common.ui.menu.events.MenuComponentEvent;
	import as3.projects.slfi.data.ExhibitVO;
	import as3.projects.slfi.components.CurrentExhibitList;
	import fl.transitions.easing.*;
	public class AllExhibits extends Sprite {
		private var list:TweenedGridList;
		private var bg:MovieClip;
		private var arrItems:Array;
		private var arrLeft:SimpleButton;
		private var arrRight:SimpleButton;
		private var svo:SectionVO;
		private var inited:Boolean = false;
		function AllExhibits(){
			addEventListener(Event.ADDED_TO_STAGE, initializeComponent);	
			addEventListener(Event.REMOVED_FROM_STAGE, cleanMeUp);
		}
		private function cleanMeUp(evt:Event):void{
			try{
				list.removeEventListener("listReady", listReady);
				removeEventListener(Event.ADDED_TO_STAGE, initializeComponent);	
			}catch(e:Error){
				
			}
		}
		private function initializeComponent(evt:Event):void{
			if (inited) {
				return;
			}else {
				inited = true;
			}
			bg = new MovieClip();
			addChild(bg);
			
			BitmapUtils.createBounds(bg, stage.stageWidth, stage.stageHeight, 0);
			
			var tmp:GalleryByMonth = new GalleryByMonth(new Object(), new TextFormat());
			list = new TweenedGridList("bdz.projects.slab.renderers.GalleryByMonth");
			list.addEventListener(MenuComponentEvent.CLICK, itemClicked);
			list.xpad = 30;
			list.labelField = "section_name";
			list.viewableColumns = 4;
			list.maxrows = 1;
			list.scrollDuration = .6;
			list.delay = .01;
			list.edgeDelay = .03;
			//list.easing = Regular.easeInOut;
			if(arrItems){
				list.arrayItems = arrItems;
			}
			
			addChild(list);
			setUpArrows();
			
		}
		private function setUpArrows():void{
			
			if (list.maxhscroll) {
				
				arrLeft = new arrowLeft();
				arrRight = new arrowRight();
				arrLeft.x = list.x-30;
				arrRight.x = 620;
				arrLeft.y = arrRight.y = 35;
				arrLeft.visible = false;
				arrRight.alpha = arrLeft.alpha = 0;
				TweenLite.to(arrRight, .8, {alpha:1});
				TweenLite.to(arrLeft, .8, {alpha:1});
				addChild(arrLeft);
				addChild(arrRight);
				arrRight.addEventListener(MouseEvent.CLICK, scrollRight);
				arrLeft.addEventListener(MouseEvent.CLICK, scrollLeft);
			}
		}
		private function scrollRight(evt:MouseEvent):void{
			
			list.scrollLeft();
			if(list.curhscroll == list.maxhscroll){
				arrRight.visible = false;
			}
			if(list.curhscroll){
				arrLeft.visible = true;
			}
		}
		private function itemClicked(evt:MenuComponentEvent):void{
			svo = evt.data as SectionVO;
			dispatchEvent(new SlabSiteEvent(SlabSiteEvent.ITEM_SELECTED, svo));
		}
		private function scrollLeft(evt:MouseEvent):void{
			
			list.scrollRight();
			if(list.curhscroll < list.maxhscroll){
				arrRight.visible = true;
			}
			if(!list.curhscroll){
				arrLeft.visible = false;
			}
		}
		private function listReady(evt:Event):void{	
			TweenLite.to(list, .8, {x:(stage.stageWidth/2-list.width/2)}, 0, setUpArrows);
		}
		private function showExhibitDetails(evt:Event):void{
			
		}
		public function setExhibitData(d:DMSCollection):void{
			if(list){
				list.arrayItems = d;
			}
			arrItems = d;
		}
		public function get selectedVO():SectionVO{
			return svo;
		}
		
		
	}
	
}